import { useEffect, useState } from "react";
import { MouseConfigInfo } from "../types/types.d";
import { MouseProfile, MouseKey } from "../types/types.v1.d";
import _ from "lodash";
import defaultDataConfig from "@/src/config/mouse_info_v1000.json";

export const defaultProfiles: MouseProfile[] = [
  {
    name: "onboardConfig",
    active: true,
    mouseKeys: defaultDataConfig.DefaultProfile.mouseKeys,
    defaultConfig: defaultDataConfig.DefaultProfile.defaultConfig,
  },
];

const idPrefix = "mc_mouse_profiles_";

export type ProfileReturnProps = {
  profiles: MouseProfile[];
  setProfiles: Function;
  initProfiles: Function;
  createProfile: Function;
  renameProfile: Function;
  delProfile: Function;
  changeProfile: Function;
  activeProfile: Function;
  saveProfiles: Function;
  updateMouseKeys: Function;
  updateMouseConfig: Function;
};

function useProfile(): ProfileReturnProps {
  const [profiles, setProfiles] = useState<MouseProfile[]>([]);
  const [deviceId, setDeviceId] = useState("");

  const initProfiles = (deviceId: string) => {
    setDeviceId(deviceId);

    const res = localStorage.getItem(idPrefix + deviceId);

    if (res) {
      const profiles = JSON.parse(res);
      console.log("profiles", profiles);
      if (profiles) {
        setProfiles(profiles);
        return true;
      }
    }
    setProfiles(defaultProfiles);
  };

  const createProfile = (name: string, index: number) => {
    profiles.push({ ...profiles[index], name, active: false });
    setProfiles(_.cloneDeep(profiles));
  };

  const renameProfile = (index: number, name: string) => {
    profiles[index].name = name;
    setProfiles(_.cloneDeep(profiles));
  };

  const delProfile = (index: number) => {
    if (profiles[index].active) {
      if (index == 0) {
        profiles[index + 1].active = true;
      } else {
        profiles[index - 1].active = true;
      }
    }
    profiles.splice(index, 1);
    setProfiles(_.cloneDeep(profiles));
  };

  const changeProfile = (index: number, newProfile: MouseProfile) => {
    const newProfiles = profiles.map((profile, idx) =>
      idx == index
        ? { ...newProfile, active: true }
        : { ...profile, active: false }
    );

    setProfiles(_.cloneDeep(newProfiles));
  };

  const activeProfile = (index: number) => {
    setProfiles((prev) =>
      prev.map((profile, idx) =>
        idx === index
          ? { ...profile, active: true }
          : { ...profile, active: false }
      )
    );
  };

  const saveProfiles = (profiles: MouseProfile[]) => {
    if (profiles != undefined && profiles.length > 0) {
      localStorage.setItem(idPrefix + deviceId, JSON.stringify(profiles));
    }
  };

  // 更新按键信息
  const updateMouseKeys = (profileIndex: number, mouseKeys: MouseKey[]) => {
    if (profileIndex < 0 || profileIndex >= profiles.length) return;
    setProfiles((prev) =>
      prev.map((profile, index) =>
        index === profileIndex ? { ...profile, mouseKeys } : profile
      )
    );
  };

  // 更新鼠标配置
  const updateMouseConfig = (
    profileIndex: number,
    defaultConfig: MouseConfigInfo
  ) => {
    if (profileIndex < 0 || profileIndex >= profiles.length) return;
    setProfiles((prev) =>
      prev.map((profile, index) =>
        index === profileIndex ? { ...profile, defaultConfig } : profile
      )
    );
  };

  useEffect(() => {
    if (profiles && profiles.length > 0) {
      saveProfiles(profiles);
    }
  }, [profiles]);

  return {
    profiles,
    setProfiles,
    initProfiles,
    createProfile,
    renameProfile,
    delProfile,
    changeProfile,
    activeProfile,
    saveProfiles,
    updateMouseKeys,
    updateMouseConfig,
  };
}

export default useProfile;
